﻿using Volo.Abp.Modularity;
using Volo.Abp;
using Volo.Abp.AspNetCore.SignalR;
using Volo.Abp.Swashbuckle;
using SignalRAbpDemo.Hub;

namespace SignalRAbpDemo
{

    [DependsOn(
    typeof(AbpAspNetCoreSignalRModule), //Add the new module dependency
         typeof(AbpSwashbuckleModule)
    )]
    public class SignalRModule : AbpModule
    {
        /// <summary>
        /// ConfigureServices
        /// </summary>
        /// <param name="context"></param>
        public override void ConfigureServices(ServiceConfigurationContext context)
        {

            context.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            context.Services.AddEndpointsApiExplorer();

            //context.Services.AddScoped<MessageHub, IAbpDemo>();

            // 添加 CORS 服务
            context.Services.AddCors(options =>
            {
                options.AddPolicy("AllowVueApp",
                    builder =>
                    {
                        builder.WithOrigins("http://localhost:5039") // 你的 Vue 应用地址
                               .AllowAnyMethod()
                               .AllowAnyHeader()
                               .AllowCredentials();
                    });
            });

            context.Services.AddAbpSwaggerGen();
        }

        /// <summary>
        /// pipeline
        /// </summary>
        /// <param name="context"></param>
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();

            if (env.IsDevelopment())
            {
                app.UseSwagger();
                app.UseAbpSwaggerUI();
            }

            // 静态文件中间件
            app.UseStaticFiles();

            app.UseCors("AllowVueApp");

            // 路由中间件
            app.UseRouting();

            // 确保所有配置的中间件和端点都被正确的添加到请求管道中
            app.UseConfiguredEndpoints(endpoits => {

                //endpoits.MapHub<MessageHub>("/signalr-hubs/message");
                //endpoits.MapHub<MessagingHub>("/signalr-hubs/messaging");
                //endpoits.MapHub<MessagingHub>("/messaging");
            });
        }
    }
}
